package com.example.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource ds;
    static {
        try{
//            1.加载配置文件
            Properties pro = new Properties();
//            使用ClassLoad加载配置文件，获取输入字节流
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);
//            初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(pro);
        }catch (IOException e){
            throw new RuntimeException("找不到druid.properties文件",e);
        }catch (Exception e){
            throw new RuntimeException("数据库初始化异常");
        }
    }
    public static DataSource getDataSource(){
        return ds;
    }
    public static Connection getConnection() throws SQLException{
        return ds.getConnection();
    }
}
